package com.tqq.onlinetest.controller;

import com.tqq.onlinetest.pojo.Student;
import com.tqq.onlinetest.pojo.StudentInfo;
import com.tqq.onlinetest.pojo.Teacher;
import com.tqq.onlinetest.pojo.TeacherInfo;
import com.tqq.onlinetest.service.StudentService;
import com.tqq.onlinetest.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/LoginController")
@CrossOrigin(origins = "*",maxAge = 3600)
public class LoginController {
    @Autowired
    TeacherService teacherService;

    @Autowired
    StudentService studentService;

    /*
     * Map包含的键值
     * msg：登陆成功/用户名或密码错误
     * status：true/false
     * obj：登录信息对象
     * roleType：登录者身份 教师：0||学生：1
     * */
    Map<String, Object> map = new HashMap<>();


    @ResponseBody
    @RequestMapping("/login")
    public Map<String, Object> Login(HttpServletRequest httpServletRequest) {

        HttpSession httpSession = httpServletRequest.getSession();
        String userType = httpServletRequest.getParameter("userType");
        String userId = httpServletRequest.getParameter("userId");
        String password = httpServletRequest.getParameter("password");
        if (userType.equals(0)) {
            Teacher user = teacherService.getTeacherLoginById(userId);
            //用户存在
            if (user != null) {
                //密码正确
                if (user.getTeacherPwd().equals(password)) {
                    TeacherInfo teacher = teacherService.getTeacherInfoById(userId);
                    httpSession.setAttribute("roleType", 0);
                    httpServletRequest.setAttribute("loginInfo", teacher);
                    map.put("msg", "登陆成功");
                    map.put("status", true);
                    map.put("obj", teacher);
                    map.put("roleType", 0);
                } else {
                    map.put("status", false);
                    map.put("msg", "密码错误");

                }
            } else {
                map.put("status", false);
                map.put("msg", "用户名不存在");
            }
        } else {
            Student student = studentService.getStudentLoginById(userId);
            if (student != null) {
                if (student.getStudentPwd().equals(password)) {
                    StudentInfo studentInfo = studentService.getStudentInfoByID(userId);
                    httpSession.setAttribute("roleType", 1);
                    httpServletRequest.setAttribute("loginInfo", studentInfo);
                    map.put("msg", "登陆成功");
                    map.put("status", true);
                    map.put("obj", studentInfo);
                    map.put("roleType", 1);
                } else {
                    map.put("status", false);
                    map.put("msg", "密码错误");
                }
            } else {
                map.put("status", false);
                map.put("msg", "用户名不存在");
            }
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/isLogin")
    public Map<String, Object> isLogin(String userId, int roleType) {
        //学生
        if (roleType == 1) {
            if (studentService.getStudentInfoByID(userId) != null) {
                map.put("msg", "登陆成功");
                map.put("status", 200);
            } else {
                map.put("msg", "未找到用户");
                map.put("status", 404);
            }
        } else {
            if (teacherService.getTeacherInfoById(userId) != null) {
                map.put("msg", "登陆成功");
                map.put("status", 200);
            } else {
                map.put("msg", "未找到用户");
                map.put("status", 404);
            }

        }
        return map;
    }
}